import { encode, decode } from 'js-base64'

export const setStora = (key: string, data: any, type: boolean | number) => {
  if (Object.prototype.toString.call(type) !== '[object Boolean]') type = true
  if (!key) throw new Error('你倒是写个key丫!')
  data = JSON.stringify(data)
  if (type) window.localStorage.setItem(encode(key), encode(data))
  else window.sessionStorage.setItem(encode(key), encode(data))
}

export const getStora = (key: string, type: any) => {
  if (Object.prototype.toString.call(type) !== '[object Boolean]') type = true
  if (!key) throw new Error('你倒是写个key丫!')
  let data
  if (type) data = window.localStorage.getItem(encode(key))
  else data = window.sessionStorage.getItem(encode(key))
  if (!data) return null
  return JSON.parse(decode(data))
}

export const removeStora = (key: string, type: any) => {
  if (Object.prototype.toString.call(type) !== '[object Boolean]') type = true
  if (!key) throw new Error('你倒是写个key丫!')
  if (type) window.localStorage.removeItem(encode(key))
  else window.sessionStorage.removeItem(encode(key))
}
